<script setup lang="ts">
import IconSvg from "@/components/IconSvg/index.vue";
import { ITableData } from "./data";
import { queryList, exportExcel, editStatus } from "./service";
import { IResponseData } from "@/@types/utils.request";
import { SearchParams } from "./data";
import searchBox from "./components/searchBox.vue";
import detailsBox from "./components/details.vue";
import { reactive, ref, onMounted } from "vue";
import { uniformPageSize } from "@/config/settings";
import { ElLoading } from "element-plus";

const table = reactive<ITableData>({
	loading: false,
	list: [],
	pagination: {
		total: 0,
		pageNum: 1,
		pageSize: 10,
	},
});
// 搜索
const searchData = ref<SearchParams>({ patientName: "", patientPhone: "", serviceType: "" });

// 服务类型
const serviceType = ref<Array<any>>([
	{
		id: 1,
		name: "图文咨询",
	},
	{
		id: 2,
		name: "上门指导",
	},
]);

// 列表数据
const getList = async (pageNum: number, pageSize?: number): Promise<void> => {
	table.loading = true;

	try {
		const response: IResponseData<any> = await queryList({
			pageSize: pageSize || table.pagination.pageSize,
			pageNum: pageNum,
			...searchData.value,
		});
		const { data } = response;
		console.log(data);
		table.list = data.records || [];
		table.pagination = {
			...table.pagination,
			total: data.total || 0,
			pageNum,
		};
	} catch (error: any) {
		console.log(error);
	}
	table.loading = false;
};

// 导出数据
const selectionData = ref<any[]>([]);
const handExport = async () => {
	try {
		const loading = ElLoading.service({
			lock: true,
			text: "导出中...",
			background: "rgba(0, 0, 0, 0.7)",
		});
		const response: IResponseData<any> = await exportExcel({
			pageSize: table.pagination.pageSize,
			pageNum: table.pagination.pageNum,
			Ids: selectionData.value,
			...searchData.value,
		});
		const { data } = response;
		console.log(data);
		loading.close();
		window.location.href = data;
	} catch (error: any) {
		console.log(error);
	}
};

const selectionChange = (val: any) => {
	console.log(val);
	selectionData.value = val.map((item: any) => item.evaluateId);
};
// 搜索
const onSearch = (val: any) => {
	searchData.value = val;
	selectionData.value = [];
	getList(1);
};

// 修改状态
const handStatusChange = async (evaluateId: number) => {
	try {
		const response: IResponseData<any> = await editStatus({ evaluateId });
		const { code, msg } = response;
		if (code === 0) {
			ElMessage({
				message: msg,
				type: "success",
			});
		} else {
			ElMessage({
				message: msg,
				type: "error",
			});
		}
	} catch (error: any) {
		console.log(error);
	}
};

const handleSizeChange = () => {
	getList(table.pagination.pageNum, table.pagination.pageSize);
};

const handleCurrentChange = (val: number) => {
	getList(val);
};

// 详情弹窗
const showVisible = ref(false);
const showData = ref<any>({});
const handShowChange = (row: any) => {
	showData.value = row;
	showVisible.value = true;
};

onMounted(() => {
	getList(1);
});
const route = useRoute();
</script>

<template>
	<searchBox v-model="searchData" :serviceType="serviceType" @search="onSearch" />
	<detailsBox v-model="showVisible" :showData="showData" />
	<el-card shadow="never">
		<template #header>
			<el-row justify="space-between">
				<el-col :span="8" style="font-size: 18px"> <IconSvg name="modular" />&nbsp;{{ route.meta.title }}</el-col>
				<el-col :span="8" style="text-align: end">
					<el-button type="primary" @click="handExport"
						><template #icon><IconSvg name="sign-out" /> </template> 导出excel</el-button
					>
				</el-col>
			</el-row>
		</template>

		<div style="width: 99.9%">
			<el-table
				row-key="id"
				:data="table.list"
				v-loading="table.loading"
				stripe
				border
				@selection-change="selectionChange"
			>
				<el-table-column type="selection" width="55" />
				<el-table-column
					type="index"
					label="序号"
					:index="
						(index: number) => {
							return (table.pagination.pageNum - 1) * table.pagination.pageSize + index + 1;
						}
					"
					sex
					width="55"
				>
				</el-table-column>
				<el-table-column label="就诊人姓名" prop="patientName"> </el-table-column>
				<el-table-column label="性别" prop="patientSex"></el-table-column>
				<el-table-column label="就诊人手机号" prop="patientPhone"> </el-table-column>
				<el-table-column label="订单号" prop="orderNo"> </el-table-column>
				<el-table-column label="服务类型" prop="serviceType"> </el-table-column>
				<el-table-column label="营养项目" prop="projectName"> </el-table-column>
				<el-table-column label="临床医师" prop="doctorName"> </el-table-column>
				<el-table-column label="营养师" prop="nutritionistName"> </el-table-column>
				<el-table-column label="订单创建时间" prop="orderCreateTime"> </el-table-column>
				<el-table-column label="订单完成时间" prop="orderEndTime"> </el-table-column>
				<el-table-column label="是否展示" prop="isShow"
					><template #default="{ row }">
						<el-switch
							v-model="row.isShow"
							:active-value="1"
							:inactive-value="0"
							@change="handStatusChange(row.evaluateId)"
						/>
					</template>
				</el-table-column>
				<el-table-column label="评价分数" prop="score" width="146">
					<template #default="{ row }">
						<el-rate v-model="row.score" disabled allow-half />
					</template>
				</el-table-column>

				<el-table-column label="操作" prop="action" width="100">
					<template #default="{ row }">
						<el-button type="primary" link @click="handShowChange(row)"
							><template #icon><IconSvg name="file" /></template>查看</el-button
						>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<div class="padding-t10 display-flex-end">
			<el-pagination
				background
				layout="total,sizes, prev, pager, next"
				v-model:current-page="table.pagination.pageNum"
				v-model:page-size="table.pagination.pageSize"
				:page-sizes="uniformPageSize"
				:total="table.pagination.total"
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
			>
			</el-pagination>
		</div>
	</el-card>
</template>
